/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.forms;

import beans.MessagesController;
import datamodel.Category;
import datamodel.SystemAdmin;
import datamodel.SystemCustomer;
import datamodel.SystemUser;
import ejb.CategoryManager;
import ejb.UserManager;
import helpers.Hasher;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author eXtreme
 */
@ManagedBean
@RequestScoped
public class NewUserForm {

    private String name;
    private String surname;
    private String role;
    private String email;
    private String password;
    private List<Long> categories;
            
    @EJB
    private UserManager um;
    
    @EJB
    private CategoryManager cm;
    
    public String saveUser() {
        if(um.exists(email)){
            MessagesController.addError("Uživatel s tímto emailem již existuje");
            return null;
        }
        SystemUser u;
        String cryptedPass = Hasher.sha256(password);
        if (UserManager.ROLE_ADMIN.equals(role)) {
            SystemAdmin sa = new SystemAdmin(name, surname, email, cryptedPass);
            List<Category> tmp = cm.getCategories(categories);
            sa.setCanSolve(tmp);
            u = sa;
        } else {
            u = new SystemCustomer(name, surname, email, cryptedPass);
        }
        um.saveUser(u);
        return "userList?faces-redirect=true";
    }

    public String getEmail() {
        return email;
    }
    
    public boolean getDisableCategories() {
        return UserManager.ROLE_CUSTOMER.equals(role);
    }

    public List<Long> getCategories() {
        return categories;
    }

    public void setCategories(List<Long> categories) {
        this.categories = categories;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }
}
